<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/19 0019
 * Time: 下午 1:30
 */

namespace App\Http\Controllers;


use App\GetTokenQn;
use Gregwar\Captcha\CaptchaBuilder;
use Session;
use Illuminate\Http\Request;

class BaseController extends Controller
{
    /**
     * 验证码生成
     * @param  [type] $tmp [description]
     * @return [type]      [description]
     */
    public function captcha($tmp)
    {
        //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder();
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();
        //把内容存入session
        Session::flash('milkcaptcha', $phrase);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
    }
//    /**
//     * 上传图片
//     * @param Request $request
//     * @return array
//     */
//    public function upload(Request $request)
//    {
//        $allowedExts = array("jpeg", "jpg", "png");
//        $folder = $request->all()['folder'];
//        $temp = explode(".", $_FILES["file"]["name"]);
//        $extension = end($temp);        // 获取文件后缀名
//        if ((($_FILES["file"]["type"] == "image/gif")
//                || ($_FILES["file"]["type"] == "image/jpeg")
//                || ($_FILES["file"]["type"] == "image/jpg")
//                || ($_FILES["file"]["type"] == "image/pjpeg")
//                || ($_FILES["file"]["type"] == "image/x-png")
//                || ($_FILES["file"]["type"] == "image/png"))
//            && ($_FILES["file"]["size"] < 5242880)    // 小于 5M
//            && in_array($extension, $allowedExts))
//        {
//            if ($_FILES["file"]["error"] > 0)
//            {
//                return ['code'=>0,'data'=>'错误：'.$_FILES["file"]["error"]];
//            } else {
//                $url = '/img/admin/'.$folder.'/';
//                $imgName = 'bing-'.time().'.jpg';
//                $urlNaem = move_uploaded_file($_FILES["file"]["tmp_name"],public_path().$url.$imgName);
//                if ($urlNaem) {
//                    return ['code'=>1,'data'=>[
//                        'thumb'=> $url.$imgName,
//                        'image'=>$imgName
//                    ]];
//                } else {
//                    return ['code'=>0,'data'=>'上传失败'];
//                }
//            }
//        } else {
//            return ['code'=>0,'data'=>'格式错误'];
//        }
//    }
    /**
     * @param Request $request
     * @return array
     * 七牛云上传文件
     */

    public function upload()
    {
        $file = $_FILES["file"];
        $GetTokenQn = new GetTokenQn();
        $FileKey = $GetTokenQn->uploadFile($file);
        return $FileKey;
    }


    /**
     * 上传图片
     * @param $files 图片
     * @param $loca 上传图片位置位置
     * @param $r 状态
     * @return array
     */
    public function uploadLocalImage($files,$loca,$r=false)
    {
        $allowedExts = array("jpeg", "jpg", "png");
        $temp = explode(".", $files['name']);
        $extension = end($temp);        // 获取文件后缀名
        if ((($files["type"] == "image/gif")
                || ($files["type"] == "image/jpeg")
                || ($files["type"] == "image/jpg")
                || ($files["type"] == "image/pjpeg")
                || ($files["type"] == "image/x-png")
                || ($files["type"] == "image/png"))
            && ($files["size"] < 5242880)    // 小于 5M
            && in_array($extension, $allowedExts))
        {
            if ($files["error"] > 0)
            {
                return ['code'=>0,'data'=>'错误：'.$files["error"]];
            } else {
                $url = '/img/'.$loca.'/';
                $imgName = date('YmdH',time()).str_pad(mt_rand(1, 99999999), 8, '0', STR_PAD_LEFT).'.jpg';
                $urlNaem = move_uploaded_file($files["tmp_name"],public_path().$url.$imgName);
                if ($urlNaem) {
                    if ($r){
                        return 'http://'.$_SERVER['HTTP_HOST'].''.$url.$imgName;
                    }else{
                        return ['code'=>1,'data'=>'','url'=>$url.$imgName];
                    }
                } else {
                    return ['code'=>0,'data'=>'上传失败'];
                }
            }
        } else {
            return ['code'=>0,'data'=>'格式错误'];
        }
    }


    /**
     * 多张图上传处理
     * @param $files 图片文件
     * @return array
     */
    public function multipleImages($files){
        $file_map = array();
        foreach ($files as $key=>$val){
            foreach ($val as $key2=>$val2){
                $file_map[$key2][$key] = $val2;
            }
        }
        return $file_map;
    }

    /**
     * @param Request $request
     * @return array
     * 七牛云删除文件
     */

    public function delFile( Request $request )
    {
        $data = $request->all();
        if (empty($data["key"])){
            return ['code'=>0,'data'=>'请上传图片的KEY'];
        }
        $GetTokenQn = new GetTokenQn();
        $res = $GetTokenQn->delFile( $data["key"] );
        return $res;
    }


    /**
     * @param Request $request
     * @return string
     * 七牛云获取访问文件
     */

    public function getFile( Request $request )
    {
        $data = $request->all();
        $GetTokenQn = new GetTokenQn();
        $res = $GetTokenQn->getDownloadUrl( $data["key"] );
        return $res;
    }

    //获取视频上传凭证
    public function getToken()
    {
        $tokenModel = new GetTokenQn();
        $res = $tokenModel->getVideoToken();
        return $res;
    }

}